<?php

use app\modules\Admin\models\memberpickgoodscardModel;
use app\modules\Admin\models\pickgoodscardModel;
use Illuminate\Database\Capsule\Manager as DB;

#[已发放提货卡管理]
class MemberpickgoodscardController extends AcommonController
{
    public function init() {
        $this->model = (new memberpickgoodscardModel())->withTrashed();
        $this->table = 'member_pickgoods_card';
        $this->primaryKey = 'id';
        parent::init();
    }

    #[提货卡列表]
    public function indexAction($returnData = false)
    {
        if($this->isPost()){
            $this->model = $this->model->with(['member', 'pickgoods_card']);
            $post_data = $this->postData;
            $post_data['mode'] = $post_data['mode']??'all';
            $search_data = [];
            if($post_data['mode'] !== 'all'){
                switch ($post_data['mode']){
                    case 'alltime':
                        $this->model = $this->model->whereNull('expire');
                        break;
                    case 'ex90day':
                        $this->model = $this->model->whereDate('expire', '>=', timestamp(time()+3600*24*90));
                        break;
                    case 'in90day':
                        $this->model = $this->model->whereDate('expire', '>=', timestamp(time()+3600*24*60))->whereDate('expire', '<', timestamp(time()+3600*24*90));
                        break;
                    case 'in60day':
                        $this->model = $this->model->whereDate('expire', '>=', timestamp(time()+3600*24*30))->whereDate('expire', '<', timestamp(time()+3600*24*60));
                        break;
                    case 'in30day':
                        $this->model = $this->model->whereDate('expire', '>=', timestamp(time()+3600*24*7))->whereDate('expire', '<', timestamp(time()+3600*24*30));
                        break;
                    case 'in7day':
                        $this->model = $this->model->whereDate('expire', '>=', timestamp(time()+3600*24*1))->whereDate('expire', '<', timestamp(time()+3600*24*7));
                        break;
                    case 'in1day':
                        $this->model = $this->model->whereDate('expire', '>=', timestamp())->whereDate('expire', '<', timestamp(time()+3600*24*1));
                        break;
                    case 'notday':
                        $this->model = $this->model->whereDate('expire', '<=', timestamp());
                        break;
                }
            }
            if(isset($post_data['memberpickgoodscard_start']) && !empty($post_data['memberpickgoodscard_start'])){
                $this->model = $this->model->whereDate('created_at', '>=', $post_data['memberpickgoodscard_start']);
            }
            if(isset($post_data['memberpickgoodscard_end']) && !empty($post_data['memberpickgoodscard_end'])){
                $this->model = $this->model->whereDate('created_at', '<=', $post_data['memberpickgoodscard_end']);
            }
            $keywordsTypeArray = [
                'card_sn' => 'card_sn',
                'user_id' => 'user_id',
                'user_phone' => 'user_phone',
                'card_id' => 'card_id',
                'card_name' => 'card_name',
            ];
            if(isset($post_data['keywordsType']) && isset($post_data['keywords'])
                && in_array($post_data['keywordsType'], $keywordsTypeArray)
                && !empty($post_data['keywords'])){
                if($post_data['keywordsType'] == $keywordsTypeArray['card_sn']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($search_data, ['card_sn', 'like', "%{$post_data['keywords']}%"]);
                    }
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['user_id'] && is_numeric($post_data['keywords']) && intval($post_data['keywords']) > 0){
                    array_push($search_data, ['member_id', '=', $post_data['keywords']]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['user_phone']){
                    if(mb_strlen($post_data['keywords'], 'utf-8') < 4){
                        array_push($search_data, ['id', '=', 0]);
                    }else{
                        array_push($this->condition_in, ['member_id', memberModel::query()->where('phone', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray()]);
                    }
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['card_id'] && is_numeric($post_data['keywords']) && intval($post_data['keywords']) > 0){
                    array_push($search_data, ['card_id', '=', $post_data['keywords']]);
                }
                if($post_data['keywordsType'] == $keywordsTypeArray['card_name'] && mb_strlen($post_data['keywords']) > 0){
                    array_push($this->condition_in, ['card_id', pickgoodscardModel::query()->where('name', 'like', "%{$post_data['keywords']}%")->pluck('id')->toArray()]);
                }
            }
            unset($keywordsTypeArray);
            $this->search = $search_data;
        }
        parent::indexAction($returnData);
    }

    #[冻结提货卡]
    public function deleteAction()
    {
        $id = $this->get($this->primaryKey, 0);
        if (empty($id)) {
            ret(2, '参数为空');
        }
        $remark = $this->get('remark', '无');
        $delModel = $this->model->where('id', $id)->where(function ($query){
            $query->whereNull('deleted_at')->orWhereDate('deleted_at', '<=', '0000-00-00');
        })->first();
        if(!$delModel){
            ret(4, '要冻结的提货卡不存在');
        }
        $cardModel = pickgoodscardModel::query()->where('id', $delModel->card_id)->first();
        if(!$cardModel){
            ret(5, '提货卡不存在');
        }
//        if(bccomp($delModel->money, $cardModel->amount) !== 0){
//            ret(6, '此提货卡已被使用，无法冻结');
//        }
//        if(strtotime($delModel->expire) < time()){
//            ret(6, '此提货卡已被过期，无法冻结');
//        }
//        if(\app\modules\Admin\models\memberpickgoodslogModel::query()->where('member_pickgoods_card_id', $id)->where('event', 'transfer')->count()){
//            ret(7, '此提货卡已被转让过，无法冻结');
//        }
        if($delModel->delete()){
            \app\modules\Admin\service\Memberpickgoodscardlog::addLog([
                'member_pickgoods_card_id' => $id,
                'event' => 'recover',
                'event_id' => 0,
                'before' => $delModel->money,
                'money' => 0,
                'after' => $delModel->money,
                'remark'     => $remark,
                'oper_type'  => 'admin',
                'oper_id'    => $this->auth->user_id,
            ]);
            unset($delModel);
            unset($remark);
            unset($cardModel);
            unset($id);
            ret(0, '提货卡冻结成功！');
        }else{
            ret(1, '提货卡冻结失败');
        }

    }

    #[解冻提货卡]
    public function restoreAction()
    {
        $id = $this->get($this->primaryKey, 0);
        if (empty($id)) {
            ret(2, '参数为空');
        }
        $remark = $this->get('remark', '无');
        $delModel = $this->model->withTrashed()->where('id', $id)->first();
        if(!$delModel){
            ret(4, '要解冻的提货卡不存在');
        }
        $cardModel = pickgoodscardModel::query()->where('id', $delModel->card_id)->first();
        if(!$cardModel){
            ret(5, '提货卡不存在');
        }
        if($delModel->restore()){
            \app\modules\Admin\service\Memberpickgoodscardlog::addLog([
                'member_pickgoods_card_id' => $id,
                'event' => 'restore',
                'event_id' => 0,
                'before' => $delModel->money,
                'money' => 0,
                'after' => $delModel->money,
                'remark'     => $remark,
                'oper_type'  => 'admin',
                'oper_id'    => $this->auth->user_id,
            ]);
            unset($delModel);
            unset($remark);
            unset($cardModel);
            unset($id);
            ret(0, '提货卡冻结成功！');
        }else{
            ret(1, '提货卡冻结失败');
        }

    }
}